#include "BlockQueue.hpp"
#include "Task.hpp"
#include <unistd.h>
void *producer(void *arg)
{
    // int data = 1;
    //  BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(arg);
    // BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(arg);

    int x = 1;
    int y = 1;

    while (1)
    {
        sleep(1);
        //  std::cout<<"生产了一个数据 "<<data<<std::endl;
        // bq->Equeue(data);
        // data++;

        // std::cout << "生产了一个任务" << x << "+" << y << "=?" << std::endl;
        // Task t(x, y);
        // bq->Equeue(t);
        // x++;
        // y++;

        std::cout << "生产了一个任务" <<std::endl;
        bq->Equeue(DownLoad);
    }
}

void *consumer(void *arg)
{
    // BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(arg);
    // BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(arg);

    while (1)
    {
        //sleep(1);
        // int data = bq->Pop();
        // std::cout<<"消费了一个数据 "<<data<<std::endl;

        // Task t = bq->Pop();
        // t.Execute();
        // std::cout << "消费了一个任务" << t.GetResult() << std::endl;

        task_t task = bq->Pop();
        task();
        std::cout << "消费了一个任务" << std::endl;
    }

}

int main()
{
    // BlockQueue<int> *bq = new BlockQueue<int>();
    // BlockQueue<Task> *bq = new BlockQueue<Task>();
    BlockQueue<task_t> *bq = new BlockQueue<task_t>();

    pthread_t c, p;

    pthread_create(&p, NULL, producer, bq);
    pthread_create(&c, NULL, consumer, bq);

    pthread_join(c, NULL);
    pthread_join(p, NULL);

    delete bq;

    return 0;
}